<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 1.在整个script标签或者整个js文件都能访问的变量,属于'全局变量',作用范围就是全局作用域,页面关闭,变量才会被销毁.
        let num = 123
        function fn(){
            console.log(num)
        }
        fn()

        // 2.局部作用域就是定义局部变量的地方,执行完毕以后,内存会立刻回收局部变量.局部变量会很少占用内存
         function fn2(){
            let str = 123
         }
         fn2()
         console.log(str); // str is not defined(访问不到)

         // 3.作用域链就是变量的逐级查找机制,遵循的原则就是就近原则
         let a = 111

         function fn3(){
            let a = 222

            function fn4(){
                // let a = 333
                console.log(a); 
            }
            fn4()
         }
         fn3()
    </script>
</body>
</html>